<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Category extends Controller_Index_Base
{
	public function action_index()
	{ 
        //print_r($_GET);
       
        $url  = $this->request->param('url') ? $this->request->param('url') : null;
        $page = $this->request->param('page') ? $this->request->param('page') : 1;
        
        $brand      = (isset($_GET['brand']) && $_GET['brand']) ? $_GET['brand'] : null; 
        $from_price = (isset($_GET['from_price']) && $_GET['from_price']) ? $_GET['from_price'] : null;
        $to_price   = (isset($_GET['to_price']) && $_GET['to_price']) ? $_GET['to_price'] : null;
        $score      = (isset($_GET['score']) && $_GET['score']) ? $_GET['score'] : null; 
        
        $category = ORM::factory('category')->get_category($url);       
        $brands   = ORM::factory('brand')->get_brands($category);
        $prices   = ORM::factory('price')->get_available_prices();
        $scores   = ORM::factory('model')->get_available_scores();
        
        $content = new View('index/category/index_tpl'); 
        $content->category = $category;
        $content->brands   = $brands;
        $content->prices   = $prices;
        $content->scores   = $scores;
        $content->models   = ORM::factory('model')->get_models($category, $brand, array('from' => $from_price, 'to' => $to_price), $score, $page);
        
        $count = $category->count_models;
        if ($brand || $from_price || $to_price || $score) {
            $count = ORM::factory('model')->get_models($category, $brand, array('from' => $from_price, 'to' => $to_price), $score, $page, true);    
        }
        $content->pagination = array(                                                   
            'page' => $page,
            'per_page' => Model_Model::get_models_per_page(),
            'count' => $count,
        );
        $content->filters = array(
            'brand'      => $brand,
            'from_price' => $from_price,
            'to_price'   => $to_price,
            'score'      => $score,
        );
        $content->text = Helper_Index::get_category_index_text($category);    
        $title = $category->name;
        if ($page > 1) {
            $title .= ' - Страница '.$page;    
        }  
        $description = Helper_Index::get_category_index_description($category);         
        $this->print_template($title, $content, $description);
	}
} 
